#include "mstar_tv.h"

MSTAR_TV::MSTAR_TV()
{
     count_dtvb=0;
     count_dtvc=0;

     old_cou_dtvb=0;
     old_cou_dtvc=0;
     del_count_dtvb=0;
     del_count_dtvc=0;

     count_atv=0;
     empty=0;
     //read_atv=false;
     mstar_atv=(MSTAR_ATV_DATA*)malloc(STRUCT_LEN_MS_A);
     mstar_dtv_data=NULL;

}


MSTAR_TV::~MSTAR_TV()
{
    free(mstar_atv);
    mstar_atv=NULL;
    for(int i=0;i<mstar_dtv.size();i++)
        free(mstar_dtv.at(i));
    mstar_dtv.clear();
    mstar_dtv_data=NULL;
}


uint MSTAR_TV::Get_DTV_Count()
{

    return count_dtvb+count_dtvc;
}
int MSTAR_TV::Read_ATV(const char *disk)
{
    char *buf=new char[FILE_PATH_LEN];
    memset(buf,0,FILE_PATH_LEN);
     /*if(!mstar_atv)
     {
           qDebug("in MSTAR_TV,mstar_atv is NULL,and malloc also error");
           return -1;
     }else
     {
        memset(mstar_atv,0,STRUCT_LEN_MS_A);
     }*/
     strcpy(buf,disk);
     strcat(buf,"database\\atv_cmdb.bin");
     atv_file=buf;
     FILE *fp=fopen(buf,"rb");
      if(!fp)
      {
               qDebug("open  file atv error");
           return -1;
      }

      uchar pBuf[FILE_LEN_MS_A];
      fread(pBuf,1,FILE_LEN_MS_A,fp);
      Q_ASSERT(mstar_atv!=NULL);
      memcpy(mstar_atv,pBuf,STRUCT_LEN_MS_A);
     for(int i=0;i<256;i++)
      {

          qDebug("%d",mstar_atv->ATVProgramData[i].misc.Unused);
        // qDebug("%s\t %d",mstar_atv->ATVProgramData[i].ui_sName,mstar_atv->ATVProgramData[i].misc.u_IsProgExist);
      }
      //qDebug("%d",mstar_atv->ui_lastPRNumber);
      fclose(fp);
     // read_atv=true;
      Get_ATV_Count();
      delete buf;
      buf=NULL;
      return 1;
}
//Read File until Channel Number as 0.the data after  0 Channel Number as invalid data.
//from 0.....max
int MSTAR_TV::Get_Max_ATV_Count()
{

    for(int i=0;i<200;i++)
    {
        int j=mstar_atv->ui_ATVPRIndexTable[i];
        if(j==253)
            return i-1;
        if(mstar_atv->ATVProgramData[j].misc.Unused==0)
        {
            return i;
        }
    }

    return -1;
}

uint  MSTAR_TV::Read_DTV_Bin(const char*dtv_file)
{
     //mstar_dtv.clear();
     uint tmp_count=0;
     if(dtv_file==NULL)
     {
      qDebug("dtv_file is Empty");
          return -2;
     }
     //QFile *fp()
     FILE *fp=fopen(dtv_file,"rb+");
     if(!fp)
     {
      qDebug("open file dtv error");
          return -1;
     }



  while(!feof(fp))
     {
      char pBuf[FILE_LEN_MS_D];
      fseek(fp,MSTAR_DTV_BIN_HEAD_LEN+FILE_LEN_MS_D*tmp_count,SEEK_SET);
      fread(pBuf,1,FILE_LEN_MS_D,fp);

          mstar_dtv_data=(MSTAR_DTV_DATA*)malloc(STRUCT_LEN_MS_D);
      if(!mstar_dtv_data)
      {
           qDebug("IN MSTAR_TV::READ_DTV_BIN  malloc error");
               return -2;
      }
     memset(mstar_dtv_data,0,STRUCT_LEN_MS_D);
     memcpy(mstar_dtv_data,pBuf,STRUCT_LEN_MS_D);
         //qDebug("%s",mstar_dtv_data->ui_ServiceName);
     if(mstar_dtv_data->ui2_Number<=0)
       {
         break;
      }
    // qDebug("%d",mstar_dtv_data->);
      mstar_dtv.append(mstar_dtv_data);
      tmp_count++;

     }
  qDebug("In MSTAR_TV::Read_DTV:%d",mstar_dtv.size());
  fclose(fp);
return tmp_count;
}


int MSTAR_TV::Read_DTV(const char *disk)
{
    char *dtvb_name=new char[FILE_PATH_LEN];
    memset(dtvb_name,0,FILE_PATH_LEN);
    strcpy(dtvb_name,disk);
    strcat(dtvb_name,"database\\dtv_cmdb_0.bin");
    dtvb_file=dtvb_name;
    count_dtvb=Read_DTV_Bin(dtvb_name);
    old_cou_dtvb=count_dtvb;
    delete dtvb_name;
    dtvb_name=NULL;

    char *dtvc_name=new char[FILE_PATH_LEN];
    memset(dtvc_name,0,FILE_PATH_LEN);
    strcpy(dtvc_name,disk);
    strcat(dtvc_name,"database\\dtv_cmdb_1.bin");
    dtvc_file=dtvc_name;
     count_dtvc= Read_DTV_Bin(dtvc_name);
     old_cou_dtvc=count_dtvc;
     delete dtvc_name;
     dtvc_name=NULL;
     del_count_dtvb=0;
     del_count_dtvc=0;
     if(count_dtvb<=0&&count_dtvc<=0)
         return -1;

     Sort_DTV();
     return 1;
}


bool Sort_By_Type(MSTAR_DTV_DATA *a,MSTAR_DTV_DATA *b)
{
    return a->u_ServiceType<b->u_ServiceType;
}



void MSTAR_TV::Count_Type(uint start,uint end,MS_TV_INDEX *tmp)
{
    memset(tmp,0,sizeof(MS_TV_INDEX));
    for(uint i=start;i<end;i++)
    {
        if(mstar_dtv.at(i)->u_ServiceType==1)
            tmp->dtv_d++;
        else if(mstar_dtv.at(i)->u_ServiceType==2)
            tmp->dtv_r++;
        else if(mstar_dtv.at(i)->u_ServiceType==3)
            tmp->dtv_t++;
        else
            tmp->dtv_u++;
    }
}

bool Sort_By_Num(MSTAR_DTV_DATA*a,MSTAR_DTV_DATA *b)
{
    return a->ui2_Number<b->ui2_Number;
}
void MSTAR_TV::Sort_Num(int start,MS_TV_INDEX tmp)
{
    if(tmp.dtv_d>0)
        qSort(mstar_dtv.begin()+start,mstar_dtv.begin()+start+tmp.dtv_d,Sort_By_Num);
    if(tmp.dtv_r>0)
        qSort(mstar_dtv.begin()+start+tmp.dtv_d,mstar_dtv.begin()+start+tmp.dtv_d+tmp.dtv_r,Sort_By_Num);
    if(tmp.dtv_t>0)
        qSort(mstar_dtv.begin()+start+tmp.dtv_d+tmp.dtv_r,mstar_dtv.begin()+start+tmp.dtv_d+tmp.dtv_r+tmp.dtv_t,Sort_By_Num);
    if(tmp.dtv_u>0)
        qSort(mstar_dtv.begin()+start+tmp.dtv_d+tmp.dtv_r+tmp.dtv_t,mstar_dtv.begin()+start+tmp.dtv_d+tmp.dtv_r+tmp.dtv_t+tmp.dtv_u,Sort_By_Num);
}

void  MSTAR_TV::Sort_DTV()
{
    qSort(mstar_dtv.begin(),mstar_dtv.begin()+count_dtvb+del_count_dtvb,Sort_By_Type);
    qSort(mstar_dtv.begin()+count_dtvb+del_count_dtvb,mstar_dtv.end(),Sort_By_Type);
    Count_Type(0,count_dtvb+del_count_dtvb,&dtvb_type);
    Count_Type(count_dtvb+del_count_dtvb,count_dtvb+count_dtvc+del_count_dtvb+del_count_dtvc,&dtvc_type);
    Sort_Num(0,dtvb_type);
    Sort_Num(count_dtvb+del_count_dtvb,dtvc_type);

}

int  MSTAR_TV::Write_ATV(char *disk)
{
    char *file_name=new char[FILE_PATH_LEN];
    memset(file_name,0,FILE_PATH_LEN);
    strcpy(file_name,disk);
    strcat(file_name,"database\\atv_cmdb.bin");

     FILE *fp=fopen(file_name,"wb+");
     if(!fp)
     {
      qDebug("in write_atv open atv file error");
      return -1;
     }

     char *pBuf=new char[FILE_LEN_MS_A];

     if(!pBuf)
     {
      qDebug("IN MSTAR_TV::Write_TV mstar_atv_data is　NULL");
      return -1;
     }
     memset(pBuf,0,FILE_LEN_MS_A);
     memcpy(pBuf,mstar_atv,STRUCT_LEN_MS_A);
     fwrite(pBuf,1,FILE_LEN_MS_A,fp);
     fflush(fp);
     fclose(fp);
//clear atv data in mstar_tv class
     delete pBuf;
     pBuf=NULL;
     delete file_name;
     file_name=NULL;
     return 1;
}

void MSTAR_TV::Copy_File(const char *src, const char *dst, const uint src_start, const uint dst_start,const uint length)
{
    FILE *fp_src=fopen(src,"rb+");


    if(!fp_src)
    {

        qDebug("In MSTAR_TV::Copy_File open file %s  failed\n",src);
        return ;
    }

    fseek(fp_src,src_start,SEEK_SET);


    uchar pBuf[length];
    memset(pBuf,0,length);
    fread(pBuf,1,length,fp_src);
    fclose(fp_src);

    FILE *fp_dst=fopen(dst,"wb+");
    if(!fp_dst)
    {
        qDebug("In MSTAR_TV::Copy_File open file %s failed\n",dst);
        return ;
    }
    if(dst_start>0)
        fseek(fp_dst,0,SEEK_END);//由于每个记录频道信息的结构体之间存在字节对齐的问题。比如，6个DTV数据，在文件写完是到1957B，而通过计算传来的是1960这个是默认处理字节对齐
    else
        fseek(fp_dst,dst_start,SEEK_SET);
    fwrite(pBuf,1,length,fp_dst);
    fflush(fp_dst);

    fclose(fp_dst);


}


int MSTAR_TV::Write_DTV_Bin(const char *file,const uint count,uint start)
{
    if(!file)
    {
        qDebug("In MSTAR_TV::Write_DTV_Bin para file is NULL");
        return -1;
    }
     FILE *fp=fopen(file,"ab");
     if(!fp)
     {
      qDebug("in write_dtv_bin open file error");
      return -1;
     }
     uint i=start;
     for(;i<count;i++)
     {
         // fseek(fp,MSTAR_DTV_BIN_HEAD_LEN+i*FILE_LEN_MS_D,SEEK_SET);
      mstar_dtv_data=mstar_dtv.at(i);
      char pBuf[FILE_LEN_MS_D];
      memset(pBuf,0,FILE_LEN_MS_D);
      memcpy(pBuf,mstar_dtv_data,STRUCT_LEN_MS_D);
      fwrite(pBuf,1,FILE_LEN_MS_D,fp);
      fflush(fp);
     }
     fclose(fp);
return 1;
}

 int MSTAR_TV::Write_DTV(char *disk)
{
     qDebug("dtvb:%d\t del_dtvb:%d",count_dtvb,del_count_dtvb);
     qDebug("dtvc:%d\tdel_dtvc:%d",count_dtvc,del_count_dtvc);

     if(!disk)
     {
         qDebug("In MSTAR_TV::Write_DTV para disk is NULL");
         return -1;
     }
     char *dtvb_name=new char[FILE_PATH_LEN];
     memset(dtvb_name,0,FILE_PATH_LEN);
     strcpy(dtvb_name,disk);
     strcat(dtvb_name,"database/cmdb_0_tmp.bin");
     char *src_b=new char[FILE_PATH_LEN];
     memset(src_b,0,FILE_PATH_LEN);
     strcpy(src_b,disk);
     strcat(src_b,"database/dtv_cmdb_0.bin");

     FILE *fp=fopen(src_b,"rb");
     if(!fp)
     {
        qDebug("in MSTAR_TV::Write_DTV open file dtv_cmdb_0.bin error");
        return -1;
     }
     //get file length
     long length=0;
     fseek(fp,0,SEEK_END);
     length=ftell(fp);


     fseek(fp,0,SEEK_SET);
     char pBuf1[MSTAR_DTV_BIN_HEAD_LEN];
     fread(pBuf1,1,MSTAR_DTV_BIN_HEAD_LEN,fp);

     FILE *newfp=fopen(dtvb_name,"wb+");
     if(!newfp)
     {
        qDebug("in MSTAR_TV::Write_DTV open file  %s error",dtvb_name);
        return -1;
     }
     fwrite(pBuf1,1,MSTAR_DTV_BIN_HEAD_LEN,newfp);
     fflush(newfp);
     fclose(newfp);


     Write_DTV_Bin(dtvb_name,count_dtvb+del_count_dtvb,0);

     newfp=fopen(dtvb_name,"ab");
     if(!newfp)
     {
        qDebug("in MSTAR_TV::Write_DTV open file  %s error",dtvb_name);
        return -1;
     }

     //here fseek ,I use old_cou_dtvb just to avoid a bug when appearred as users del the last row datas in QList
     const int last_len1=length-(MSTAR_DTV_BIN_HEAD_LEN+FILE_LEN_MS_D*(count_dtvb+del_count_dtvb));//file offset start at 0
     if(last_len1>0)
     {
        fseek(fp,MSTAR_DTV_BIN_HEAD_LEN+FILE_LEN_MS_D*(count_dtvb+del_count_dtvb),SEEK_SET);
        char pBuf2[last_len1];
        fread(pBuf2,1,last_len1,fp);
        fclose(fp);
        //fseek(newfp,0,SEEK_END);
        fseek(fp,MSTAR_DTV_BIN_HEAD_LEN+FILE_LEN_MS_D*(count_dtvb+del_count_dtvb),SEEK_SET);
        fwrite(pBuf2,1,last_len1,newfp);
        fflush(newfp);
        fclose(newfp);

     }else
     {
        fclose(fp);
        fclose(newfp);
     }
     delete dtvb_name;
     dtvb_name=NULL;
     delete src_b;
     src_b=NULL;

     char *dtvc_name=new char[FILE_PATH_LEN];
     memset (dtvc_name,0,FILE_PATH_LEN);
     strcpy(dtvc_name,disk);
     strcat(dtvc_name,"database/cmdb_1_tmp.bin");
     char *src_c=new char[FILE_PATH_LEN];
     memset(src_c,0,FILE_PATH_LEN);
     strcpy(src_c,disk);
     strcat(src_c,"database/dtv_cmdb_1.bin");

    // Copy_File(src,dtvc_name,0,0,MSTAR_DTV_BIN_HEAD_LEN);

            fp=fopen(src_c,"rb+");
             if(!fp)
             {
                qDebug("in MSTAR_TV::Write_DTV open file dtv_cmdv_1.bin error");
                return -1;
             }
             //get file length
             fseek(fp,0,SEEK_END);
             length=ftell(fp);

             fseek(fp,0,SEEK_SET);
             char pBuf3[MSTAR_DTV_BIN_HEAD_LEN+1];
             fread(pBuf3,1,MSTAR_DTV_BIN_HEAD_LEN,fp);
             newfp=fopen(dtvc_name,"wb+");
             if(!newfp)
             {
                qDebug("in MSTAR_TV::Write_DTV open file %s error",dtvc_name);
                return -1;
             }
             fwrite(pBuf3,1,MSTAR_DTV_BIN_HEAD_LEN,newfp);
             fflush(newfp);
             fclose(newfp);

             Write_DTV_Bin(dtvc_name,mstar_dtv.size(),count_dtvb+del_count_dtvb);

             newfp=fopen(dtvc_name,"ab");
             if(!newfp)
             {
                qDebug("in MSTAR_TV::Write_DTV open file %s error",dtvc_name);
                return -1;
             }
             const int last_len2=length-(MSTAR_DTV_BIN_HEAD_LEN+FILE_LEN_MS_D*(count_dtvc+del_count_dtvc));
             if(last_len2>0)
             {
                fseek(fp,MSTAR_DTV_BIN_HEAD_LEN+FILE_LEN_MS_D*(count_dtvc+del_count_dtvc),SEEK_SET);
                char pBuf4[last_len2];
                fread(pBuf4,1,last_len2,fp);
                fclose(fp);
                fwrite(pBuf4,1,last_len2,newfp);
                fclose(newfp);
             }else
             {
                fclose(fp);
                fclose(newfp);
             }
        delete dtvc_name;
        dtvc_name=NULL;
        delete src_c;
        src_c=NULL;
return 1;
  }

uint MSTAR_TV::Get_ATV_Count()
{
   count_atv=256;
   return count_atv;
}


//set atv u_wPLL as 0,then the data be invalid.
//The Row is index
void MSTAR_TV::Del_ATV_Row(int row)
{

    mstar_atv->ATVProgramData[row].misc.u_Hide=1;
    //找到下标

    int max_atv=Get_Max_ATV_Count();

    for(int i=row;i<max_atv;i++)
    {
        mstar_atv->ui_ATVPRIndexTable[i]=mstar_atv->ui_ATVPRIndexTable[i+1];
    }
    mstar_atv->ui_ATVPRIndexTable[max_atv]=253;
    /*for(int i=max_atv-1;i<200;i++)
    {
        mstar_atv->ui_ATVPRIndexTable[i]=mstar_atv->ui_ATVPRIndexTable[i+1];
    }
    for(int i=200;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
    {
        mstar_atv->ui_ATVPRIndexTable[i]=0;
    }*/
     // mstar_atv->ui_ATVPRIndexTable[row]=0;
   /* switch(row%8)
    {
    case 0:
         mstar_atv->ui_ATVPRTableMap[row/8]&=254;
         break;
    case 1:
        mstar_atv->ui_ATVPRTableMap[row/8]&=253;
        break;
    case 2:
        mstar_atv->ui_ATVPRTableMap[row/8]&=251;
        break;
    case 3:
        mstar_atv->ui_ATVPRTableMap[row/8]&=247;
        break;
     case 4:
        mstar_atv->ui_ATVPRTableMap[row/8]&=239;
        break;
    case 5:
        mstar_atv->ui_ATVPRTableMap[row/8]&=223;
        break;
    case 6:
        mstar_atv->ui_ATVPRTableMap[row/8]&=191;
        break;
    case 7:
        mstar_atv->ui_ATVPRTableMap[row/8]&=127;
        break;
     }
     */
    // memcpy(&mstar_atv->ATVProgramData[max_atv],&tmp,sizeof(ST_ATV_PROGRAM_DATA));
}
//remove the data form qList,del the row,the other datas will be OK
//in here ,wo need the dtv class can't mnodify
void MSTAR_TV::Del_DTV_Row(int row,int tv_class)
{
    Q_ASSERT(row<mstar_dtv.size());


    mstar_dtv.at(row)->u_IsDelete=1;
   if(tv_class==0)
   {
        count_dtvb--;
        del_count_dtvb++;
        /*int mov_to=count_dtvb;
        if(mstar_dtv.at(row)->u_ServiceType==1)
        {
         //   dtvb_type.dtv_d--;
            mov_to=dtvb_type.dtv_d;
        }
        else if(2==mstar_dtv.at(row)->u_ServiceType)
        {
          //  dtvb_type.dtv_r--;
            mov_to=dtvb_type.dtv_r;
        }
         else if(3==mstar_dtv.at(row)->u_ServiceType)
          {
            //dtvb_type.dtv_t--;
            mov_to=dtvb_type.dtv_t;
        }else
        {
           // dtvb_type.dtv_u--;
            mov_to=dtvb_type.dtv_u;
        }
        mstar_dtv.move(row,mov_to);*/
   }
   else
   {    del_count_dtvc++;
        count_dtvc--;
       /* int mov_to=count_dtvc;
        if(mstar_dtv.at(row)->u_ServiceType==1)
        {
           // dtvc_type.dtv_d--;
            mov_to=count_dtvb+del_count_dtvb+dtvc_type.dtv_d;
        }
        else if(2==mstar_dtv.at(row)->u_ServiceType)
        {
            //dtvc_type.dtv_r--;
            mov_to=count_dtvb+del_count_dtvb+dtvc_type.dtv_r;
        }
         else if(3==mstar_dtv.at(row)->u_ServiceType)
          {
           // dtvc_type.dtv_t--;
            mov_to=count_dtvb+del_count_dtvb+dtvc_type.dtv_t;
        }else
        {
            //dtvc_type.dtv_u--;
            mov_to=count_dtvb+del_count_dtvb+dtvc_type.dtv_u;
        }
        mstar_dtv.move(row,mov_to);*/
   }
       memset(mstar_dtv.at(row),0,sizeof(MSTAR_DTV_DATA));
}

void MSTAR_TV::Mod_ATV(int row,int col,QString value)
{
    uint freq;
    bool Flag=false;
    bool *ok=&Flag;
    ushort ch_num;
    switch(col)
    {
    case 0:
        /*ch_num=value.toShort(ok);
        Q_ASSERT(*ok==true);
        Swap_ATV_CH(row,ch_num-1);//交换第row和ch_num的数据
        mstar_atv->ATVProgramData[ch_num-1].misc.u_IsProgExist=0;
        mstar_atv->ATVProgramData[row].misc.u_IsProgExist=1;
        correct(row,ch_num-1);*/
        break;
    case 1:
        memcpy(mstar_atv->ATVProgramData[row].ui_sName,value.toStdString().c_str(),
               strlen(value.toStdString().c_str()));
        qDebug("%s",mstar_atv->ATVProgramData[row].ui_sName);
        break;
    case 2:
        if(0==value.compare("air"))
        mstar_atv->ATVProgramData[row].misc.u_Medium=0;
        else
            mstar_atv->ATVProgramData[row].misc.u_Medium=1;
        //Q_ASSERT(*ok==true);
        break;
    case 3:
        freq=value.toInt();
        mstar_atv->ATVProgramData[row].ui2_wPLL=freq*100/STEPSIZE;
        break;
    case 4:
        mstar_atv->ATVProgramData[row].misc.u_Favorite=value.toUShort(ok);
        Q_ASSERT(*ok==true);
        break;
    case 5:
        mstar_atv->ATVProgramData[row].misc.u_Skip=value.toUShort(ok);
        Q_ASSERT(*ok==true);
        break;
    default:
            qDebug("in  MSTAR_TV::Mod_ATV para col error");
    }
}
//usually checksum very big,it will not below 0,but here is dangerous
void MSTAR_TV::Mod_Check(ushort num1, ushort num2)
{
    mstar_dtv_data->ui4_CheckSum+=num2;
    mstar_dtv_data->ui4_CheckSum-=num1;
}

void MSTAR_TV::Mod_DTV(int row, int col, QString value)
{
    //if(row>count_dtvb)
      //  mstar_dtv_data=mstar_dtv.at(row);
    bool Flag=false;
    bool *ok=&Flag;
    ushort Table_ID;
    mstar_dtv_data=mstar_dtv.at(row);
    switch(col)
    {
    case 0:

     Mod_Check(mstar_dtv_data->ui2_Number,value.toUShort(ok));
     Q_ASSERT(*ok==true);
     mstar_dtv_data->ui2_Number=(ushort)value.toUShort();
        break;
    case 1:
    {
        string str=value.toStdString();
        const uint len=str.length();
        const uint length=strlen((const char *)mstar_dtv_data->ui_ServiceName);
        for(uint i=0;i<MAX_SERVICE_NAME;i++)
        {
           // uint num=(uint)str.at(i);
            if(i<len)
                mstar_dtv_data->ui4_CheckSum+=str.at(i);
            //num=mstar_dtv_data->ui_ServiceName[i];
            if(i<length)
                mstar_dtv_data->ui4_CheckSum-=mstar_dtv_data->ui_ServiceName[i];

            if(i>=len&&i>=length)
                break;
        }
        memset(mstar_dtv_data->ui_ServiceName,0,MAX_SERVICE_NAME);
        memcpy(mstar_dtv_data->ui_ServiceName,str.c_str(),
               strlen(str.c_str()));
        //qDebug("%s",mstar_dtv_data->ui_ServiceName);
        break;
    }
    case 2://class暂时不可修改
           //数据结构中没有此项，
    case 3://不可修改，添加时使用
        ushort tv_type;
        if(0==value.compare("D"))
            tv_type=1;
        else if(0==value.compare("R"))
            tv_type=2;
        else if(0==value.compare("T"))
            tv_type=3;
        else if(0==value.compare("U"))
            tv_type=4;
        else
            tv_type=5;
        Mod_Check((ushort)mstar_dtv_data->u_ServiceType,tv_type);
        mstar_dtv_data->u_ServiceType=(uchar)tv_type;//value 这里总是在0-5之间
        break;
    case 4://Freq
       // Mod_Check((ushort)mstar_dtv_data->ui);
        Table_ID=mstar_dtv_data->ui2_MuxTableID;
        ushort new_table_id;

        if(row<=count_dtvb+del_count_dtvb)
        {
            if(!Mod_Freq(&new_table_id,value.toUInt(),true))
                QMessageBox::warning(NULL,"Waring","Sorry,Modify is invalid!");
            else
            {
                Mod_Check(Table_ID,new_table_id);
                mstar_dtv_data->ui2_MuxTableID=new_table_id;
            }
        }else
        {
            if(!Mod_Freq(&new_table_id,value.toUInt(),false))
                QMessageBox::warning(NULL,"Waring","Sorry,Modify is invalid!");
            else{
                Mod_Check(Table_ID,new_table_id);
                mstar_dtv_data->ui2_MuxTableID=new_table_id;
            }
        }
        break;
    case 5:
        Mod_Check((ushort)mstar_dtv_data->ui_Favorite,value.toUShort(ok));
        Q_ASSERT(*ok==true);
        mstar_dtv_data->ui_Favorite=value.toUShort();
        break;
    case 6:
        if(1==mstar_dtv_data->u_IsSkipped)
            mstar_dtv_data->ui4_CheckSum-=32;
        if(1==value.toUShort(ok))
        {
            mstar_dtv_data->ui4_CheckSum+=32;
            mstar_dtv_data->u_IsSkipped=1;
        }else{
            mstar_dtv_data->u_IsSkipped=0;
        }

        Q_ASSERT(*ok==true);
       // mstar_dtv_data->u_IsSkipped=value.toUShort();//char类型
        break;
    default:
        qDebug("in  MATAR_TV::Mod_DTV para col eror");
    }

 }

void MSTAR_TV::Add_DTV_CH(QList<QString>& Item)
{
    mstar_dtv_data=(MSTAR_DTV_DATA*)malloc(sizeof(MSTAR_DTV_DATA));
    QString tv_class=Item.at(2);
   // int list_num=0;
   // int way;
  //  bool Flag=false;
   // bool *ok=&Flag;
    if(0==tv_class.compare("DTV-B"))
    {
       //list_num=Find_Empty_DTV(0,&way,Item.at(4).toInt(ok))-1;
       // Q_ASSERT(*ok==true);
        memcpy(mstar_dtv_data,mstar_dtv.at(0),sizeof(MSTAR_DTV_DATA));
        mstar_dtv_data->u_IsDelete=0;
        mstar_dtv.insert(count_dtvb,mstar_dtv_data);
        for(int i=0;i<Item.size();i++)
            Mod_DTV(count_dtvb,i,Item.at(i));

      /*  ushort type=mstar_dtv_data->u_ServiceType;

        if(1==type&&dtvb_type.dtv_d>0)
        {
            dtvb_type.dtv_d++;
            mstar_dtv.insert(count_dtvb,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(0,i,Item.at(i));

            }
            //qSort(mstar_dtv.begin(),mstar_dtv.begin()+dtvb_type.dtv_d,Sort_By_Num);
        }else if(2==type&&dtvb_type.dtv_r>0)
        {
            dtvb_type.dtv_r++;
            mstar_dtv.insert(dtvb_type.dtv_d,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(dtvb_type.dtv_d,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+dtvb_type.dtv_d,mstar_dtv.begin()+dtvb_type.dtv_d+dtvb_type.dtv_r,Sort_By_Num);
        }else if(3==type&&dtvb_type.dtv_t>0)
        {
            dtvb_type.dtv_t++;
            mstar_dtv.insert(dtvb_type.dtv_d+dtvb_type.dtv_r,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(dtvb_type.dtv_d+dtvb_type.dtv_r,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+dtvb_type.dtv_d+dtvb_type.dtv_r,mstar_dtv.begin()+dtvb_type.dtv_d+dtvb_type.dtv_r+dtvb_type.dtv_t,Sort_By_Num);
        }else if(4==type&&dtvb_type.dtv_u>0)
        {
            dtvb_type.dtv_u++;
            mstar_dtv.insert(dtvb_type.dtv_d+dtvb_type.dtv_r+dtvb_type.dtv_t,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(dtvb_type.dtv_d+dtvb_type.dtv_r+dtvb_type.dtv_t,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+dtvb_type.dtv_d+dtvb_type.dtv_r+dtvb_type.dtv_t,mstar_dtv.begin()+dtvb_type.dtv_d+dtvb_type.dtv_r+dtvb_type.dtv_t+dtvb_type.dtv_u,Sort_By_Num);

        }else{
            mstar_dtv.insert(count_dtvb+del_count_dtvb,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(count_dtvb+del_count_dtvb,i,Item.at(i));

            }
        }*/

        count_dtvb++;
        old_cou_dtvb--;
    }else
    {
        //list_num=Find_Empty_DTV(1,&way,Item.at(4).toInt(ok));
        //Q_ASSERT(*ok==true);
        memcpy(mstar_dtv_data,mstar_dtv.at(count_dtvb),sizeof(MSTAR_DTV_DATA));
        mstar_dtv_data->u_IsDelete=0;

        mstar_dtv.insert(count_dtvb+del_count_dtvb+count_dtvc,mstar_dtv_data);
        for(int i=0;i<Item.size();i++)
            Mod_DTV(count_dtvb+del_count_dtvb+count_dtvc,i,Item.at(i));
/*        ushort type=mstar_dtv_data->u_ServiceType;

        int start=count_dtvb+del_count_dtvb;
        if(1==type&&dtvc_type.dtv_d>0)
        {
            dtvc_type.dtv_d++;
            mstar_dtv.insert(start,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(start,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+start,mstar_dtv.begin()+start+dtvc_type.dtv_d,Sort_By_Num);
        }else if(2==type&&dtvc_type.dtv_r>0)
        {
            dtvc_type.dtv_r++;
            mstar_dtv.insert(start+dtvc_type.dtv_d,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(start+dtvc_type.dtv_d,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+start+dtvc_type.dtv_d,mstar_dtv.begin()+start+dtvc_type.dtv_d+dtvc_type.dtv_r,Sort_By_Num);
        }else if(3==type&&dtvc_type.dtv_t>0)
        {
            dtvc_type.dtv_t++;
            mstar_dtv.insert(start+dtvc_type.dtv_d+dtvc_type.dtv_r,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(start+dtvc_type.dtv_d+dtvc_type.dtv_r,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+start+dtvc_type.dtv_d+dtvc_type.dtv_r,mstar_dtv.begin()+start+dtvc_type.dtv_d+dtvc_type.dtv_r+dtvb_type.dtv_t,Sort_By_Num);
        }else if(4==type&&dtvc_type.dtv_u>0)
        {
            dtvc_type.dtv_u++;
            mstar_dtv.insert(start+dtvc_type.dtv_d+dtvc_type.dtv_r+dtvc_type.dtv_t,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(start+dtvc_type.dtv_d+dtvc_type.dtv_r+dtvc_type.dtv_t,i,Item.at(i));

            }
            qSort(mstar_dtv.begin()+start+dtvc_type.dtv_d+dtvc_type.dtv_r+dtvc_type.dtv_t,mstar_dtv.begin()+start+dtvc_type.dtv_d+dtvc_type.dtv_r+dtvc_type.dtv_t+dtvc_type.dtv_u,Sort_By_Num);

        }else{
            mstar_dtv.insert(count_dtvb+del_count_dtvb+count_dtvc+del_count_dtvc,mstar_dtv_data);
            for(int i=0;i<Item.size();i++)
            {
                Mod_DTV(count_dtvb+count_dtvc+del_count_dtvb+del_count_dtvc,i,Item.at(i));

            }
        }*/
        count_dtvc++;
        old_cou_dtvc--;
    }
}

void MSTAR_TV::Add_ATV_CH(QList<QString> &Item)
{
   // int Max_Index_Table=0;
    //int Max_Ch_Val=Get_Max_ATV_Count();
   /*
    for(int i=0;i<MAX_PRTABLEMAP;i++)
    {
        char bitmap=mstar_atv->ui_ATVPRTableMap[i];
        if(bitmap&0x7f)
        {
            continue;
        }else if(bitmap&0xbf)
        {
            Max_Index_Table=i*8+7;
            mstar_atv->ui_ATVPRTableMap[i]|=0x80;
            break;
        }else if(bitmap&0xdf)
        {
            Max_Index_Table=i*8+6;
            mstar_atv->ui_ATVPRTableMap[i]|=0x40;
            break;
        }else if(bitmap&0xef)
        {
            Max_Index_Table=i*8+5;
            mstar_atv->ui_ATVPRTableMap[i]|=0x20;
            break;
        }else if(bitmap&0xf7)
        {
            Max_Index_Table=i*8+4;
            mstar_atv->ui_ATVPRTableMap[i]|=0x10;
            break;
        }else if(bitmap&0xfb)
        {
            Max_Index_Table=i*8+3;
            mstar_atv->ui_ATVPRTableMap[i]|=0x08;
            break;
        }else if(bitmap&0xfd)
        {
            Max_Index_Table=i*8+2;
            mstar_atv->ui_ATVPRTableMap[i]|=0x04;
            break;
        }else if(bitmap&0xfe)
        {
            Max_Index_Table=i*8+1;
            mstar_atv->ui_ATVPRTableMap[i]|=0x02;
            break;
        }else {
            mstar_atv->ui_ATVPRTableMap[i]=0x01;
            break;
        }
    }*/
    int Add_Index=-1;
    int Add_Loc=-1;
    for(int i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
    {
        int j=mstar_atv->ui_ATVPRIndexTable[i];
        if(i>=200)
            break;
        //没有经过 TV端编辑的数据在有效数据之后，ui_ATVPRIndexTable存的是253
        if(j==253)
        {
            Add_Index=i;
            Add_Loc=mstar_atv->ui_ATVPRIndexTable[i-1]+1;
            break;
        }
        //经过TV端编辑后的数据，ui_ATVPRIndexTable存的事从在有效数据之后+1
        if(mstar_atv->ATVProgramData[j].misc.Unused==0)
        {
            Add_Index=i;
            Add_Loc=j;
            break;
        }
    }

    if(-1==Add_Loc)
    {
        QMessageBox::warning(NULL,"Error","Find Add loction error");
        return ;
    }
    //Max_Ch_Val=mstar_atv->ui_ATVPRIndexTable[Max_Index_Table-1];
    mstar_atv->ui_ATVPRIndexTable[Add_Index]=Add_Loc;
    //mstar_atv->ui_lastPRNumber=Max_Ch_Val+1;
    memcpy(&mstar_atv->ATVProgramData[Add_Loc],&mstar_atv->ATVProgramData[0],sizeof(ST_ATV_PROGRAM_DATA));
    for(int i=0;i<Item.size();i++)
    {
        Mod_ATV(Add_Loc,i,Item.at(i));
    }
    mstar_atv->ATVProgramData[Add_Loc].misc.Unused=1;
    mstar_atv->ATVProgramData[Add_Loc].misc.u_IsProgExist=0;
    mstar_atv->ATVProgramData[Add_Loc].misc.u_Hide=0;

}

bool MSTAR_TV::Mod_Freq(ushort *table_id, uint value,bool is_b_c)
{
    uint addr;
    QString file_name;
   // int length;
    if(is_b_c)
        file_name=dtvb_file;
    else
        file_name=dtvc_file;
    char buf[0x28];
    FILE *fp=fopen(file_name.toLatin1().data(),"rb+");
    if(!fp)
    {
        QMessageBox::warning(NULL,"Warning","Open file  error");
        return false;
    }
    //获取第一个频点数据
    addr=0x3f83c;
    fseek(fp,addr,SEEK_SET);
    fread(buf,1,0x28,fp);

    int i=0;
    for(;i<1000;i++)
    {
        addr=0x28*i+0x3f83c;


        fseek(fp,addr+16,SEEK_SET);
        char new_buf[4];
        fread(new_buf,1,4,fp);
        int freq=0;
        memcpy(&freq,new_buf,4);
        if(0==freq)
            break;
    }
    *table_id=i;
    //获取所要填写的频点后面的数据
    //fseek(fp,0,SEEK_END);
    //length=ftell(fp);
    fseek(fp,addr+0x28,SEEK_SET);
    const int last_buf_len=MSTAR_DTV_FILE_LEN-addr-0x28;
    char last_buf[last_buf_len];
    //qDebug("%d",last_buf);
    memset(last_buf,0,last_buf_len);
    fread(last_buf,1,last_buf_len,fp);

    //fflush(fp);
    //fclose(fp);

   // fp=fopen(file_name.toLatin1().data(),"wb+");
    //利用MuxTable[0]处的数据复制给新的MuxTable,并将freq改为所需值即可
    addr=0x28*i+0x3f83c;
    fseek(fp,addr,SEEK_SET);
    char pbuf[4];
    memcpy(pbuf,&value,4);
    memcpy(buf+16,pbuf,4);
    fwrite(buf,1,0x28,fp);
    //写完剩余数据
    addr+=0x28;
    fseek(fp,addr,SEEK_SET);
    fwrite(last_buf,1,last_buf_len,fp);

    fflush(fp);
    fclose(fp);
    //delete []last_buf;
    //last_buf=NULL;
    return true;
}

bool MSTAR_TV::Is_Empty()
{
    if(empty<=0)
        return true;
    else
        return false;

}
void MSTAR_TV::Clear()
{

    delete mstar_atv;
    mstar_atv=NULL;



    //qDeleteAll(mstar_dtv);
    for(int i=0;i<mstar_dtv.size();i++)
        free(mstar_dtv.at(i));
    //delete mstar_dtv_data;
    mstar_dtv_data=NULL;
    mstar_dtv.clear();
   // read_atv=false;
    count_atv=0;
    count_dtvb=0;
    count_dtvc=0;
    old_cou_dtvb=0;
    old_cou_dtvc=0;
}
void MSTAR_TV::Clear_Data()
{
    memset(mstar_atv,0,sizeof(MSTAR_ATV_DATA));
    mstar_dtv.clear();
}
void MSTAR_TV::set_Empty(uint num)
{
    empty=num;
}

void MSTAR_TV::Empty_Add_One()
{
    empty++;
}

void MSTAR_TV::Empty_Dul_One()
{
    empty--;
}

void MSTAR_TV::Get_Chan_Num(QList<uint> *atv_Num, QMap<uint,uchar> *dtv_air_Num,QMap<uint,uchar> *dtv_cable_Num)
{
    for(int i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
    {
        int j=mstar_atv->ui_ATVPRIndexTable[i];
        if(1==mstar_atv->ATVProgramData[j].misc.u_Hide
                ||mstar_atv->ATVProgramData[j].ui2_wPLL<=0
                ||i>=200
                ||0==mstar_atv->ATVProgramData[j].misc.Unused)
            break;
        atv_Num->append(i+1);
    }
    uint i=0;
    int sum=0;
    for(;i<count_dtvb+del_count_dtvb;i++)
    {
        if(mstar_dtv.at(i)->u_IsDelete==0&&mstar_dtv.at(i)->u_ServiceType<5)
        {
            uint num=mstar_dtv.at(i)->ui2_Number;
            uchar type=mstar_dtv.at(i)->u_ServiceType;
            if(num>0&&num<=9999)
            {
                dtv_air_Num->insertMulti(num,type);
                sum++;
            }
        }
    }
    for(;i<(uint)mstar_dtv.size();i++)
    {
        if(mstar_dtv.at(i)->u_IsDelete==0&&mstar_dtv.at(i)->u_ServiceType<5)
        {
            uint num=mstar_dtv.at(i)->ui2_Number;
            uchar type=mstar_dtv.at(i)->u_ServiceType;
           // qDebug("%d\n",type);
            if(num>0&&num<=9999)
            {
                dtv_cable_Num->insertMulti(num,type);
                sum++;
            }
        }
    }
    qDebug("The useful number:%d",sum);
}


int MSTAR_TV::cal_freq(ushort table_id,bool is_b_c)
{
    uint addr=0x28*table_id+0x3f83c;
    QString file_name;
    if(is_b_c)
        file_name=dtvb_file;
    else
        file_name=dtvc_file;


    FILE *fp=fopen(file_name.toLatin1().data(),"rb");
    if(!fp)
    {
        return -1;
    }
    fseek(fp,addr+16,SEEK_SET);
    char buf[4];
    int freq;
    fread(buf,1,4,fp);
    memcpy(&freq,buf,4);
    fclose(fp);
    return freq;
}

ushort MSTAR_TV::Get_ATV_Num(ushort  i)
{
    ushort num=mstar_atv->ui_ATVPRIndexTable[i];
    if(mstar_atv->ATVProgramData[num].misc.Unused==0)
        return 0;
    return i+1;
}

ushort MSTAR_TV::Get_DTV_Num(ushort i)
{
    ushort num=mstar_dtv.at(i)->ui2_Number;
    return num;
}

uint MSTAR_TV::Get_DTV_Sum(int tv_class)
{
    if(0==tv_class)
        return count_dtvb+del_count_dtvb;
    else
        return count_dtvc+del_count_dtvc;
}

//table_id ->结构体中的MuxTableID
//is_b_c
//
bool MSTAR_TV::Is_Exist_Freq(ushort table_id,bool is_b_c)
{
    if(cal_freq(table_id,is_b_c)<0)
        return false;
    else
        return true;

}

//is_b_c true-dtv_cmdb_0.bin,false-dtv_cmdb_1.bin
//return value:if success return a negative number;if failed ,return -1;
ushort MSTAR_TV::Get_Empty_Freq(bool is_b_c)
{
    for(ushort i=0;i<MS_DTV_MAX_FRE_TABLE;i++)
    {
        if(!Is_Exist_Freq(i,is_b_c))
            return i;
    }
    QMessageBox::warning(NULL,"Warning","Frequency Table is full!");
    return -1;
}
void MSTAR_TV::Swap_ATV_CH(ushort ch_num1, ushort ch_num2)
{
    ST_ATV_PROGRAM_DATA st_atv;
    memcpy(&st_atv,&mstar_atv->ATVProgramData[ch_num2],sizeof(ST_ATV_PROGRAM_DATA));
    memcpy(&mstar_atv->ATVProgramData[ch_num2],&mstar_atv->ATVProgramData[ch_num1],sizeof(ST_ATV_PROGRAM_DATA));
    memcpy(&mstar_atv->ATVProgramData[ch_num1],&st_atv,sizeof(ST_ATV_PROGRAM_DATA));

    uchar tmp=mstar_atv->ui_ATVPRIndexTable[ch_num1];
    mstar_atv->ui_ATVPRIndexTable[ch_num1]=mstar_atv->ui_ATVPRIndexTable[ch_num2];
    mstar_atv->ui_ATVPRIndexTable[ch_num2]=tmp;
}


void MSTAR_TV::correct(ushort old_num, ushort new_num)
{
   int new_result=new_num/8;
   int new_rember=new_num%8;
   int old_result=old_num/8;
   int old_rember=old_num%8;
   if(new_num>mstar_atv->ui_lastPRNumber)
       mstar_atv->ui_lastPRNumber=new_num;
    switch(old_rember)
    {
    case 0:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&254;
        break;
    case 1:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&253;
        break;
    case 2:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&251;
        break;
    case 3:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&247;
        break;
    case 4:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&239;
        break;
    case 5:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&223;
        break;
    case 6:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&191;
        break;
    case 7:
        mstar_atv->ui_ATVPRTableMap[old_result]=mstar_atv->ui_ATVPRTableMap[old_result]&127;
    }

    switch(new_rember)
    {
    case 0:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|1;
        break;
    case 1:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|2;
        break;
    case 2:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|4;
        break;
    case 3:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|8;
        break;
    case 4:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|16;
        break;
    case 5:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|32;
        break;
    case 6:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|64;
        break;
    case 7:
        mstar_atv->ui_ATVPRTableMap[new_result]=mstar_atv->ui_ATVPRTableMap[new_result]|128;
    }
}


void MSTAR_TV::Check_ATV()
{
    int Num[256];
    int SNum[8]={1,2,4,8,16,32,64,128};
    memset(Num,-1,256);
    for(int i=0;i<32;i++)
    {
        for(int j=0;j<8;j++)
        {
            int z=mstar_atv->ui_ATVPRIndexTable[i*8+j];
            if(z==253)
                break;
            else
            {
                Num[i*8+j]=z;
            }
        }

    }
    int z=0;
    for(int i=0;i<32;i++)
    {
        mstar_atv->ui_ATVPRTableMap[i]=0;
        for(int j=0;j<8;j++)
        {
            if(Num[z]==i*8+j)
            {
                mstar_atv->ui_ATVPRTableMap[i]|=SNum[j];
                z++;
            }
        }
    }
}

//找到mstar_dtv中Is_delete 位1的数据,作为添加数据
//否则就是在原有数据的后面直接添加，这样为了保证bin文件中的数据的地址没有变化，
//仍然需要保持bin文件中的dtv1000个数目不变，则利用old_cou_dtvb/c来控制写入时的数据量
int MSTAR_TV::Find_Empty_DTV(int tv_class,int *way,int new_freq)
{
   // int i=0;
    *way=0;
    if(0==tv_class)
    {
        ushort old_table=-1;
        int j=0;
        FILE *fp=fopen(dtvb_file.toLatin1().data(),"rb+");
        if(!fp)
        {
            qDebug("Open File Failed");
            return -1;
        }
       for(uint i=0;i<count_dtvb;i++)
        {
           ushort table_id=mstar_dtv.at(i)->ui2_MuxTableID;
           if(table_id!=old_table)
           {
               fseek(fp,0x3f83c+0x28*j,SEEK_SET);
               char buf[4];
               fread(buf,1,4,fp);
               int freq;
               memcpy(&freq,buf,4);
               if(freq==new_freq)
               {
                   fclose(fp);
                   return j;
               }
               else
                   j++;
               old_table=table_id;
           }

        }
        fclose(fp);
       // count_dtvb++;
        //old_cou_dtvb++;
        *way=1;
        return count_dtvb-1;
    }else
    {
        ushort old_table=-1;
        int j=0;
        FILE *fp=fopen(dtvc_file.toLatin1().data(),"rb+");
        if(!fp)
        {
            qDebug("Open File Failed");
            return -1;
        }
       for(uint i=count_dtvb;i<count_dtvb+count_dtvc;i++)
        {
           ushort table_id=mstar_dtv.at(i)->ui2_MuxTableID;
           if(table_id!=old_table)
           {
               fseek(fp,0x3f83c+0x28*j,SEEK_SET);
               char buf[4];
               fread(buf,1,4,fp);
               int freq;
               memcpy(&freq,buf,4);
               if(freq==new_freq)
               {
                   fclose(fp);
                   return j;
               }
               else
                   j++;
               old_table=table_id;
           }

        }
        fclose(fp);
      //  count_dtvc++;
        *way=1;
        return count_dtvb+count_dtvc-1;
    }

}
